/**
 * @author Justin Akin
 * @version Action Pals Core Library 0.1.0
 */
package com.actionpals.components.controls
{	
	import com.actionpals.base.BaseSprite;
	import com.actionpals.broadcasters.KeyScrollCast;
	import com.actionpals.broadcasters.WheelScrollCast;
	import com.actionpals.display.IResizable;
	import com.actionpals.enum.Align;
	import com.actionpals.enum.Direction;
	import com.actionpals.events.KeyScrollEvent;
	import com.actionpals.events.WheelScrollEvent;
	import com.actionpals.utils.PositionUtil;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * The basic functionality for a scrollBar
	 */
	public class SimpleScrollBar extends BaseSprite
	{
		
	//--------------------------------------------------------------------------------
	//
	//	Protected Member Variables
	//
	//--------------------------------------------------------------------------------
	
		protected var _arrowA:Sprite;
		protected var _arrowB:Sprite;
		protected var _slider:GraphicalSlider;
		protected var _percentInc:Number;
		protected var _assetStackDirection:String = Direction.BELOW;
		protected var _target:DisplayObject;
		
	//--------------------------------------------------------------------------------
	//
	//	Constructor
	//
	//--------------------------------------------------------------------------------
	
		/**
		 * Constructor
		 */		
		public function SimpleScrollBar( w:Number = 10, h:Number = 50 )
		{	
			super();	
			init( w, h );
		}
		
	//--------------------------------------------------------------------------------
	//	Public Methods
	//--------------------------------------------------------------------------------
		
		/**
		 * 
		 */
		public function moveHandleToPercent( percent:Number, orientation:String = Align.LEFT ):void
		{
			_slider.moveHandleToPercent( percent, orientation );
		}
		
	//--------------------------------------------------------------------------------
	//
	//	Getters and Setters
	//
	//--------------------------------------------------------------------------------		
	
		public function get target():DisplayObject
		{
			return _target;
		}
		
		public function set target( target:DisplayObject ):void
		{
			_target = target;
		}
		
		public function get trackWidth():Number
		{
			return _slider.trackWidth;
		}
		
		public function set trackWidth( w:Number ):void
		{
			_slider.trackWidth = w;
		}
		
		public function get trackHeight():Number
		{
			return _slider.trackHeight;
		}
		
		public function set trackHeight( h:Number ):void
		{
			_slider.trackHeight = h;
		}
		
		public function get handleWidth():Number
		{
			return _slider.handleWidth;
		}
		
		public function set handleWidth( w:Number ):void
		{
			_slider.handleWidth = w;
		}
		
		public function get handleHeight():Number
		{
			return _slider.handleHeight;
		}
		
		public function set handleHeight( h:Number ):void
		{
			_slider.handleHeight = h;
		}
		
	//--------------------------------------------------------------------------------
	//
	//	Protected Methods
	//
	//--------------------------------------------------------------------------------
		/**
		 * 
		 */
		protected function init( w:Number, h:Number ):void
		{				
			addAssets( w, h );
			layout();
			addEventListener( Event.ADDED_TO_STAGE, onAddedToStage );	
		}
		
		/**
		 * 
		 */
		protected function addArrowA():void
		{	
			_arrowA = addChild( new Sprite() ) as Sprite;
		}
				
		/**
		 * 
		 */
		protected function addArrowB():void
		{	
			_arrowB = addChild( new Sprite() ) as Sprite;		
		}
			
		/**
		 * 
		 */
		protected function addSlider( w:Number, h:Number ):void{
			
			_slider = addChild( new GraphicalSlider( w, h ) ) as GraphicalSlider;
			
		}
			
		/**
		 * 
		 */
		protected function addAssets( w:Number, h:Number ):void
		{	
			var newW:Number;
			var newH:Number;
			
			addArrowA();
			addArrowB();
			
			newW = w;
			newH = h - _arrowA.height - _arrowB.height;

			addSlider( newW, newH );
			
			_slider.addEventListener( Event.CHANGE, onUpdate );
			_slider.eventsBubble = true;
		}
		
		protected function layout():void
		{
			PositionUtil.stack( _slider, _arrowA, _assetStackDirection );
			PositionUtil.stack( _arrowB, _slider, _assetStackDirection );
		}
			
		/**
		 * 
		 */
		protected function addHandleGraphic( handleGraphic:DisplayObject ):void
		{	
			_slider.addHandleGraphic( handleGraphic );	
		}
			
		/**
		 * 
		 */
		protected function addTrackGraphic( trackGraphic:DisplayObject ):void
		{
			_slider.addTrackGraphic( trackGraphic );	
		}
			
		/**
		 * 
		 */
		protected function addListeners():void
		{	
			var keyScrollCast:KeyScrollCast = new KeyScrollCast( stage );
			var wheelScrollCast:WheelScrollCast = new WheelScrollCast( stage );
			
			keyScrollCast.addEventListener( KeyScrollEvent.ARROW_UP, onArrowUp );
			keyScrollCast.addEventListener( KeyScrollEvent.ARROW_DOWN, onArrowDown );
			keyScrollCast.addEventListener( KeyScrollEvent.ARROW_LEFT, onArrowLeft );
			keyScrollCast.addEventListener( KeyScrollEvent.ARROW_RIGHT, onArrowRight );
			keyScrollCast.addEventListener( KeyScrollEvent.PAGE_UP, onPageUp );
			keyScrollCast.addEventListener( KeyScrollEvent.PAGE_DOWN, onPageDown );
			keyScrollCast.addEventListener( KeyScrollEvent.HOME, onHome );
			keyScrollCast.addEventListener( KeyScrollEvent.END, onEnd );
			
			wheelScrollCast.addEventListener( WheelScrollEvent.WHEEL_UP, onWheelUp );
			wheelScrollCast.addEventListener( WheelScrollEvent.WHEEL_DOWN, onWheelDown );
		}
		
	//--------------------------------------------------------------------------------
	//
	//	Event Handlers
	//
	//--------------------------------------------------------------------------------
			
		/**
		 * 
		 */
		protected function onAddedToStage( event:Event ):void
		{
			addListeners();
		}
				
		/**
		 * 
		 */	
		protected function onUpdate( event:Event ):void
		{
			
		}
			
		/**
		 * 
		 */
		protected function onArrowUp( event:KeyScrollEvent ):void
		{
			var percent:Number = _slider.percentFromTop - _percentInc;
			_slider.moveHandleToPercent( percent, Align.TOP );
		}
			
		/**
		 * 
		 */
		protected function onArrowDown( event:KeyScrollEvent ):void
		{
			var percent:Number = _slider.percentFromTop + _percentInc;
			_slider.moveHandleToPercent( percent, Align.TOP );
		}
			
		/**
		 * 
		 */
		protected function onArrowLeft( event:KeyScrollEvent ):void
		{
			var percent:Number = _slider.percentFromLeft - _percentInc;
			_slider.moveHandleToPercent( percent, Align.LEFT );
		}
			
		/**
		 * 
		 */
		protected function onArrowRight( event:KeyScrollEvent ):void
		{
			var percent:Number = _slider.percentFromLeft + _percentInc;
			_slider.moveHandleToPercent( percent, Align.LEFT );
		}
			
		/**
		 * 
		 */
		protected function onPageUp( event:KeyScrollEvent ):void
		{
						
		}
			
		/**
		 * 
		 */
		protected function onPageDown( event:KeyScrollEvent ):void
		{
			
		}
			
		/**
		 * 
		 */
		protected function onHome( event:KeyScrollEvent ):void
		{
			_slider.moveHandleToPercent( 0, Align.TOP );
		}
			
		/**
		 * 
		 */
		protected function onEnd( event:KeyScrollEvent ):void
		{
			_slider.moveHandleToPercent( 1, Align.TOP );
		}
			
		/**
		 * 
		 */
		protected function onWheelUp( event:WheelScrollEvent ):void
		{
			var inc:Number = event.delta * _percentInc;
			var percent:Number = _slider.percentFromTop - inc;
			
			_slider.moveHandleToPercent( percent, Align.TOP );			
		}
			
		/**
		 * 
		 */
		protected function onWheelDown( event:WheelScrollEvent ):void
		{	
			var inc:Number = event.delta * _percentInc;
			var percent:Number = _slider.percentFromTop - inc;
			
			_slider.moveHandleToPercent( percent, Align.TOP );			
		}		
	}	
}